$(document).ready(function(){
	var currentPage = 1;
	var pageSize = 10;
	var token = $.zui.store.get("token");
	var userId = $.zui.store.get("userId");
	var queryListFun = function(){
		//获取列表数据
		var queryListUrl = adminQueryUrl + areaService;
		var param = "action=queryListPage";
		if ($("#queryAreaCode").val()!=''){
			param += "&areaCode="+$("#queryAreaCode").val();
		}
		if ($("#queryAreaNameLike").val()!=''){
			param += "&areaNameLike="+$("#queryAreaNameLike").val();
		}
		param += "&pageSize="+pageSize;
		param += "&currentPage="+(currentPage-1);
		console.log('token='+token+",userId="+userId);
		console.log('queryListUrl='+queryListUrl);
		console.log('token='+token+",userId="+userId);
		console.log('param='+param);
		$.ajax({
			url:queryListUrl,
			type:'post',
			beforeSend:function(xhr){
				xhr.setRequestHeader("token",token);
				xhr.setRequestHeader("userId",userId);
			},
			header:{"token":token,"userId":userId},
			data:param,
			dataType:"JSON",
			success:function(result){
				console.log(result.success);
				console.log(result.pageList);
				if (result.success=='1'){
					$('#listDataGrid').empty();
					$('#listDataGrid').data('zui.datagrid',null);
					$('#listPager').empty();
					$('#listPager').data('zui.pager',null);
					$('#listDataGrid').datagrid({
						checkable:true,
						checkByClickRow: true,
					    dataSource: {
					        cols:[
					        	{name: 'areaId', label: '1', width: 1},
					            {name: 'areaCode', label: '地区编码', width: 132},
					            {name: 'areaName', label: '地区名称', width: 134},
					            {name: 'areaParentName', label: '上级地区名称', width: 109},
					            {name: 'sortNo', label: '排序码', width: 109},
					            {name: 'description', label: '描述', width: -1}
					        ],
					        cache:false,
					        array:result.pageList
					    }
					});	
					
					// 手动进行初始化
					$('#listPager').pager({
					    page: currentPage,
					    recPerPage:pageSize,
					    elements:['first_icon', 'prev_icon', 'pages', 'next_icon', 'last_icon', 'total_text','size_menu','goto'],
					    pageSizeOptions:[5,10,20,30,50,100],
					    recTotal: result.pageTotal
					});
				}
				else {
					new $.zui.Messager(result.msg, {
					    type: 'warning', // 定义颜色主题
					    placement: 'center' // 定义显示位置
					}).show();
				}
			},
			error:function(result){
				new $.zui.Messager('系统繁忙,请稍候再试!', {
				    type: 'warning', // 定义颜色主题
				    placement: 'center' // 定义显示位置
				}).show();
			}
		});
	};
	//调起查询数据
	queryListFun();
	//查询
	$("#searchBtn").click(function(){
		queryListFun();
	});
	//监听分页，修改当前页，条数
	$('#listPager').on('onPageChange', function(e, state, oldState) {
		currentPage = state.page;
		pageSize = state.recPerPage;
		queryListFun();
	});
	//初始化添加
	$("#addBtn").click(function(){
		$("#addForm")[0].reset();
		//获取地区列表
		var url = adminQueryUrl + areaService;
		var param = 'action=queryAllList';//请求到列表页面
		$.ajax({
			url:url,
			type:'post',
			dataType:'JSON',
			beforeSend:function(xhr){//设置请求头信息
				xhr.setRequestHeader("token:'"+token+"'");
				xhr.setRequestHeader("userId:'"+userId+"'");
			},
			headers:{'token':token,'userId':userId},
			data:param,
			success:function(data){
				console.log('data='+data);
				if (data.success=='1'){
					$.each(data.areaList, function(i, item){     
						$("#addAreaParentCode").append("<option value='"+item.areaCode+"'>"+item.areaName+"</option>");
					});  
				}
			},
			error:function(e){
				new $.zui.Messager('系统繁忙,请稍候再试!', {
				    type: 'warning',
				    placement:'center'
				}).show();
			}
		});
	});
	$("#addSave").click(function(){
		var validAreaCodeUrl = adminQueryUrl + areaService + "?action=validAreaCode";
		var validAreaNameUrl = adminQueryUrl + areaService + "?action=validAreaName";
		$("#addForm").validate({
			rules:{
				addAreaCode:{
					"required":true,
					"remote":{
						url:validAreaCodeUrl,
						type:'get',
						dataType:'json',
						beforeSend:function(xhr){//设置请求头信息
							xhr.setRequestHeader("token:'"+token+"'");
							xhr.setRequestHeader("userId:'"+userId+"'");
						},
						headers:{'token':token,'userId':userId},
						data:{
							'areaCode':function(){
								return $("#addAreaCode").val();
							}
						}
					}
				},
				addAreaName:{
					"required":true,
					"remote":{
						url:validAreaNameUrl,
						type:'get',
						beforeSend:function(xhr){//设置请求头信息
							xhr.setRequestHeader("token:'"+token+"'");
							xhr.setRequestHeader("userId:'"+userId+"'");
						},
						headers:{'token':token,'userId':userId},
						dataType:'json',
						data:{
							'areaName':function(){
								return $("#addAreaName").val();
							},
							'areaParentCode':function(){
								return $("#addAreaParentCode").val();
							}
						}
					}
				},
				addSortNo:{
					"required":true,
					"digits":true
				}
			},
			messages:{
				addAreaCode:{
					"remote":"地区编码已经存在"
				},
				addAreaName:{
					"remote":"地区名称已经存在"
				}
			},
			submitHandler:function(form){
				//form.submit();
				$.ajax({
					url:adminQueryUrl + areaService,
					type:'post',
					dataType:'JSON',
					beforeSend:function(xhr){//设置请求头信息
						xhr.setRequestHeader("token:'"+token+"'");
						xhr.setRequestHeader("userId:'"+userId+"'");
					},
					headers:{'token':token,'userId':userId},
					data:$(form).serialize(),
					success:function(data){
						console.log('data='+data);
						if (data.success == '1') {
		                	new $.zui.Messager('添加成功!', {
		    				    type: 'success',
		    				    placement:'center'
		    				}).show();
		                	$('#addModal').modal('hide', 'fit');
		                	queryAreaListFun();
		                }
		                else{
		                	new $.zui.Messager(data.msg, {
		    				    type: 'warning',
		    				    placement:'center'
		    				}).show();
		                }
					},
					error:function(e){
						new $.zui.Messager('系统繁忙,请稍候再试!', {
	    				    type: 'warning',
	    				    placement:'center'
	    				}).show();
					}
				});
			}
		});
	});
	//var arraylist = {};
	/*
	$('#myDataGrid').datagrid({
	    dataSource: {
	        cols:[
	            {name: 'areaCode', label: '地区编码', width: 132},
	            {name: 'areaName', label: '地区名称', width: 134},
	            {name: 'areaParetName', label: '上级地区名称', width: 109},
	            {name: 'sortNo', label: '排序码', width: 109},
	            {name: 'description', label: '描述', width: 287}
	        ],
	        array:[
	            {areaCode: '350000', areaName:'福建省', areaParetName: '中国', sortNo: 10, description: '福建是个好地方'}
	        ]
	        ,
	        array:arraylist
	    }
	});
	*/
	/*
	var queryListUrl = adminQueryUrl + areaService;
	var token = $.zui.store.get("token");
	var userId = $.zui.store.get("userId");
	var param = "action=queryList";
	console.log('token='+token+",userId="+userId);
	console.log('queryListUrl='+queryListUrl);
	$('#myDataGrid').datagrid({
	    dataSource: {
	        cols:[
	        	{name: 'areaCode', label: '地区编码', width: 132},
	            {name: 'areaName', label: '地区名称', width: 134},
	            {name: 'areaParetName', label: '上级地区名称', width: 109},
	            {name: 'sortNo', label: '排序码', width: 109},
	            {name: 'description', label: '描述', width: 287}
	        ],
	        remote: function(params) {
	            return {
                	url:queryListUrl,
            		type:'post',
            		beforeSend:function(xhr){
            			xhr.setRequestHeader("token",token);
            			xhr.setRequestHeader("userId",userId);
            		},
            		header:{"token":token,"userId":userId},
            		data:param,
            		dataType:"JSON"
	            };
	        }
	    }
	});
	*/
});